package org.example.lost.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.domain.PageInfoDTO;
import org.example.lost.entity.FoundInfo;
import org.example.lost.entity.LostInfo;
import org.example.lost.entity.vo.HistoryInfoVO;
import org.example.lost.entity.vo.LostInfoVO;
import org.example.lost.mapper.FoundMapper;
import org.example.lost.mapper.LostMapper;
import org.example.lost.service.FoundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author hxy
 * @Date 2021/3/21 19:30
 * @Version 1.0
 */
@Service
public class FoundServiceImpl implements FoundService {
    @Autowired
    FoundMapper foundMapper;


    @Override
    public int addFound(FoundInfo foundInfo) {
        foundInfo.setFoundId(UUID.randomUUID().toString().replace("-",""));
        foundInfo.setFoundReleaseTime(new Date());
        foundInfo.setCreateTime(new Date());
        foundInfo.setFoundStatus(0);
        foundInfo.setCheckStatus(0);
        int result = foundMapper.insertFoundInfo(foundInfo);
        return result;
    }

    @Override
    public PageInfoDTO<FoundInfo> getList(LostInfoVO lostInfoVO) {
        PageHelper.startPage(lostInfoVO.getPageNum(),lostInfoVO.getPageSize());
        List<FoundInfo> foundInfoList = foundMapper.selectList(lostInfoVO);
        PageInfo<FoundInfo> pageInfo=new PageInfo<>(foundInfoList);
        return PageInfoDTO.to(pageInfo);
    }

    @Override
    public int deleteFound(String foundId) {
        int result = foundMapper.deleteByPrimaryKey(foundId);
        return result;
    }

    @Override
    public int updateFound(FoundInfo foundInfo) {
        int result = foundMapper.updateByPrimaryKey(foundInfo);
        return result;
    }

    @Override
    public PageInfoDTO<FoundInfo> getOne(HistoryInfoVO historyInfoVO) {
        PageHelper.startPage(historyInfoVO.getPageNum(),historyInfoVO.getPageSize());
        List<FoundInfo> foundInfoList = foundMapper.selectByPrimaryKey(historyInfoVO);
        PageInfo<FoundInfo> pageInfo = new PageInfo<>(foundInfoList);
        return PageInfoDTO.to(pageInfo);
    }

    @Override
    public String claim(String userId, String foundId) {
        int result=foundMapper.updateClaim(userId,foundId);
        String userPhone = foundMapper.selectUserPhone(foundId);
        if(result == 1){
            return userPhone;
        }
        return "";
    }

    @Override
    public int updateCheckStatus(String foundId, String status) {
        return foundMapper.updateCheckStatus(foundId,status);
    }
}
